using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
    public class FAQQuestionsDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion
       
		

		#region Methods

		/// <summary>
		/// Saves a record to the FAQ_Questions table.
		/// </summary>
		public virtual int Insert(FAQQuestionsInfo fAQQuestionsInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ZoneId", fAQQuestionsInfo.ZoneId),
				new SqlParameter("@Question", fAQQuestionsInfo.Question),
				new SqlParameter("@Answer", fAQQuestionsInfo.Answer),		
				new SqlParameter("@Priority", fAQQuestionsInfo.Priority),
				new SqlParameter("@CreatedBy", fAQQuestionsInfo.CreatedBy),
				new SqlParameter("@CreatedOn", fAQQuestionsInfo.CreatedOn)
			};

			return (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "FAQ_Questions_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the FAQ_Questions table.
		/// </summary>
		public virtual void Update(FAQQuestionsInfo fAQQuestionsInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@FAQ_ID", fAQQuestionsInfo.FAQ_ID),
				new SqlParameter("@ZoneId", fAQQuestionsInfo.ZoneId),
				new SqlParameter("@Question", fAQQuestionsInfo.Question),
				new SqlParameter("@Answer", fAQQuestionsInfo.Answer),
				new SqlParameter("@Priority", fAQQuestionsInfo.Priority),
				new SqlParameter("@CreatedBy", fAQQuestionsInfo.CreatedBy),
				new SqlParameter("@CreatedOn", fAQQuestionsInfo.CreatedOn)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "FAQ_Questions_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the FAQ_Questions table by its primary key.
		/// </summary>
		public virtual void Delete(int fAQ_ID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@FAQ_ID", fAQ_ID)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "FAQ_Questions_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the FAQ_Questions table.
		/// </summary>
		public virtual FAQQuestionsInfo Select(int fAQ_ID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@FAQ_ID", fAQ_ID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "FAQ_Questions_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeFAQQuestionsInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}
     
         //<summary>
         //Selects all records from the FAQ_Questions table.
         //</summary>
        public CHRTList<FAQQuestionsInfo> SelectAll()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "FAQ_Questions_SelectAll"))
            {
                CHRTList<FAQQuestionsInfo> fAQQuestionsInfoList = new CHRTList<FAQQuestionsInfo>();
                while (dataReader.Read())
                {
                    FAQQuestionsInfo fAQQuestionsInfo = MakeFAQQuestionsInfo(dataReader);
                    fAQQuestionsInfoList.Add(fAQQuestionsInfo);
                }

                return fAQQuestionsInfoList;
            }
        }
        public CHRTList<FAQQuestionsInfo> SearchAll(string question, int status, int from, int to)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@Question",question),
                                              new SqlParameter("@Status",status),
                                              new SqlParameter("@From",from),
                                              new SqlParameter("@To",to)
		};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "FAQ_Questions_SearchAll", parameters))
            {
                CHRTList<FAQQuestionsInfo> newsLetterEmailsInfoList = new CHRTList<FAQQuestionsInfo>();
                while (dataReader.Read())
                {
                    FAQQuestionsInfo newsLetterEmailsInfo = MakeFAQQuestionsInfo(dataReader);
                    newsLetterEmailsInfoList.Add(newsLetterEmailsInfo);
                }

                return newsLetterEmailsInfoList;
            }
        }
        public int GetCount(string question, int status)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                            {
                                                new SqlParameter("@Question", question),
                                                new SqlParameter("@Status", status)
                                            };
            return
                (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "FAQ_Questions_GetCount", parameters);

        }

		/// <summary>
		/// Creates a new instance of the FAQ_Questions class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual FAQQuestionsInfo MakeFAQQuestionsInfo(SqlDataReader dataReader)
		{
			FAQQuestionsInfo fAQQuestionsInfo = new FAQQuestionsInfo();
			fAQQuestionsInfo.FAQ_ID = SqlClientUtility.GetInt32(dataReader,DbConstants.FAQ_QUESTIONS.FAQ_ID, 0);
			fAQQuestionsInfo.ZoneId = SqlClientUtility.GetInt32(dataReader,DbConstants.FAQ_QUESTIONS.ZONEID, 0);
			fAQQuestionsInfo.Question = SqlClientUtility.GetString(dataReader,DbConstants.FAQ_QUESTIONS.QUESTION, String.Empty);
			fAQQuestionsInfo.Answer = SqlClientUtility.GetString(dataReader,DbConstants.FAQ_QUESTIONS.ANSWER, String.Empty);
			fAQQuestionsInfo.Status = SqlClientUtility.GetInt32(dataReader,DbConstants.FAQ_QUESTIONS.STATUS, 0);
			fAQQuestionsInfo.Priority = SqlClientUtility.GetInt32(dataReader,DbConstants.FAQ_QUESTIONS.PRIORITY, 0);
			fAQQuestionsInfo.CreatedBy = SqlClientUtility.GetString(dataReader,DbConstants.FAQ_QUESTIONS.CREATEDBY, String.Empty);
			fAQQuestionsInfo.CreatedOn = SqlClientUtility.GetDateTime(dataReader,DbConstants.FAQ_QUESTIONS.CREATEDON, DateTime.Now);

			return fAQQuestionsInfo;
		}

		#endregion
	}
}
